package mysql

import (
	"gorm.io/gorm"
	"hema-fresh/hemarpc/hemarpc"
)

type HemaProduct struct {
	gorm.Model
	ProductName  string  `gorm:"type:varchar(30);not null;comment:商品名称"`
	ProductPrice float64 `gorm:"type:decimal(10,2);not null;comment:商品金额"`
	ProductStock int     `gorm:"type:int;not null;comment:商品库存"`
	HemaTypeId   int     `gorm:"type:int;not null;comment:商品分类ID"`
}

func (p *HemaProduct) ProductAdd(db *gorm.DB) error {
	return db.Debug().Create(p).Error
}

func (p *HemaProduct) ProductDetail(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).First(p).Error
}

func (p *HemaProduct) ProductDel(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Delete(p).Error
}

func (p *HemaProduct) ProductUpdate(db *gorm.DB) error {
	return db.Debug().Updates(p).Error
}

func ProductList(db *gorm.DB, in *hemarpc.ProductListRequest) ([]*hemarpc.ProductList, error) {
	var hema []*hemarpc.ProductList
	err2 := db.Debug().Model(&HemaProduct{}).
		Select(
			"product_name",
			"product_price",
			"product_stock",
			"hema_type_id",
			"hema_products.id as hema_product_id",
		).
		Scopes(Paginate(int(in.Page), int(in.Size))).Find(&hema).Error
	return hema, err2
}
